// pages/editProfile/editProfile.js
const app = getApp();
Page({
  data: {
    nickname: '',
    genderIndex: 0,
    genders: ['男', '女', '保密'],
    birthday: '2000-01-01',
    phone: '',
    region: ['北京市', '北京市', '东城区']
  },
  
   /* 弹出三级选择器 */
   showRegionPicker() {
    wx.navigateTo({
      url: '/pages/regionPicker/regionPicker?region=' +
           encodeURIComponent(JSON.stringify(this.data.region))
    });
  },

  /* 选择器页面返回的回调 */
  onRegionConfirm(e) {
    // 新增打印
    console.log('selected region', e.detail);
    this.setData({ region: e.detail });
  },

 
  onLoad() {
    const userInfo = wx.getStorageSync('userInfo') || {};
    this.setData({
      nickname: userInfo.nickname || '',
      phone: userInfo.phone || '',
      region: userInfo.region || ['北京市', '北京市', '东城区']
    });
  },

  goBack() {
    wx.navigateBack();
  },

  onNicknameInput(e) {
    this.setData({ nickname: e.detail.value });
  },

  onGenderChange(e) {
    this.setData({ genderIndex: parseInt(e.detail.value) });
  },

  onBirthdayChange(e) {
    this.setData({ birthday: e.detail.value });
  },

  copyPhone() {
    wx.setClipboardData({
      data: this.data.phone,
      success: () => wx.showToast({ title: '已复制', icon: 'none' })
    });
  },

  showRegionPicker() {
    wx.navigateTo({
      url: '/pages/regionPicker/regionPicker?region=' + encodeURIComponent(JSON.stringify(this.data.region))
    });
  },

  // 地区选择器返回
  onRegionConfirm(e) {
    this.setData({ region: e.detail });
  },

  saveProfile() {
    const userInfo = wx.getStorageSync('userInfo') || {};
    const newUserInfo = {
      ...userInfo,
      nickname: this.data.nickname,
      phone: this.data.phone,
      region: this.data.region
    };
    wx.setStorageSync('userInfo', newUserInfo);

    wx.showToast({ title: '已保存', icon: 'success' });
    // 返回 mine 页面
    wx.navigateBack();
  }
});